package controller

import (
	"fmt"
	"gin/common"
	"gin/common/constant"
	commonLog "gin/common/log"

	//"gin/dao"
	"gin/system/entity"
	"gin/system/service"
	"github.com/gin-gonic/gin"
/*	"github.com/dgrijalva/jwt-go"
	"sync"
*/
)

//登录校验，生成token，并返回token
func Login(context *gin.Context) {
	loginBody := &entity.LoginBody{}
	context.BindJSON(&loginBody)
	//list := service.Login(context, loginBody)
	common.Try(func() {
		loginToken := service.Login(context, loginBody)
		common.ReturnLoginSucess(context, constant.SUCCESSCODE(), constant.SUCCESSMSG(), loginToken)
	}, func(err interface{}) {
		common.ReturnLoginSucess(context, constant.ERRORCODE(), "token生成失败，请联系管理员", "")
	})
}

//用户退出
func Logout(c *gin.Context) {
	logger := commonLog.InitLogger()

	sysUser := service.GetProfile1(c)
	fmt.Println("UUID=============================" + sysUser.Uuid)
	blacklist := service.NewBlacklist()
	logger.Println(constant.LOGINFO() + "用户名称为："+sysUser.UserName +"; 退出成功！！！")

	// 将token添加到黑名单
	blacklist.AddToken(sysUser.Token)
}

func Rsapassword(context *gin.Context) {
	loginBody := &entity.LoginBody{}
	context.BindJSON(&loginBody)
	list := service.Rsapassword(context, loginBody)

	i := len(list)
	fmt.Print(i)
	common.ReturnSucess(context, constant.SUCCESSCODE(), constant.SUCCESSMSG(), list, int64(len(list)))
}
